package cn.edu.gdut.csq.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.edu.gdut.csq.entity.Companyinfo;
import cn.edu.gdut.csq.entity.Job;
import cn.edu.gdut.csq.entity.PageSet;

@Component
public class JobDao {

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;

	public Session openSession() {
		return sessionFactory.openSession();
	}

	public Session getSession() {

		// 事务必须是开启的(Required)，否则获取不到

		return sessionFactory.getCurrentSession();

	}
	/**
	 * 根据 id 获取  job
	 * @param jobId
	 * @return
	 */
	public Job getJobById(Integer jobId) {
		// TODO Auto-generated method stub

		String hql = "from Job j order by j.queryTime desc";
		Query query = getSession().createQuery(hql);
		List<Job> jobs = query.list();
		System.out.println(jobs.size());

		if (jobId > jobs.size()) {
			return null;
		}
		return (Job) getSession().get(Job.class, jobId);
	}

	/**
	 *  分页  获取  Jobs
	 * @param pageNow
	 * @param pageSize
	 * @return
	 */
	public PageSet<Job> getJobs(Integer pageNow, Integer pageSize) {
		// TODO Auto-generated method stub

		String hql = "from Job j order by j.queryTime desc";

		return this.getJobsBySql(pageNow, pageSize, hql);
	}

	/**
	 *  按时间  分页 获取 job
	 * @param pageNow
	 * @param pageSize
	 * @param time
	 * @return
	 */
	public PageSet<Job> getJobs(Integer pageNow, Integer pageSize,
			String time) {
		// TODO Auto-generated method stub
		String hql = "from Job j where j.publishTime like '" + time + "%' order by j.queryTime desc";

		return this.getJobsBySql(pageNow, pageSize, hql);
	}
	
	/**
	 * 根据 sql 查询 job
	 * @param pageNow
	 * @param pageSize
	 * @param hql
	 * @return
	 */
	public PageSet<Job> getJobsBySql(Integer pageNow, Integer pageSize,
			String hql) {
		Query query = getSession().createQuery(hql)
				.setFirstResult((pageNow - 1) * pageSize)
				.setMaxResults(pageSize);
		List<Job> Jobs = query.list();
		PageSet<Job> pagejobs = new PageSet<Job>();
		pagejobs.setCurrentPage(pageNow);
		pagejobs.setList(Jobs);
		pagejobs.setNumPerPage(pageSize);
		Query query2 = getSession().createQuery(hql);

		int allRow = query2.list().size();
		pagejobs.setAllRow(allRow);
		pagejobs.setTotalPage(PageSet.countTotalPage(pageSize, allRow));
		System.out.println("totalpage" + pagejobs.getTotalPage());
		return pagejobs;
	}
	
	

	/**
	 * 按时间   导出   job
	 * @param time
	 * @return
	 */
	public List<Job> exportJobsbyTime(String time) {

		// TODO Auto-generated method stub
		String hql = "from Job j where j.publishTime like '" + time + "%' order by j.queryTime desc";

		Query query = getSession().createQuery(hql);
		List<Job> jobs = query.list();
		return jobs;
	}

}
